// pages/loginPhone/loginPhone.js
var app = getApp()
wx.cloud.init()
// const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sendText: "发送验证码",
    authorize: ""
  },
  loginPhone: function(e) { //提交登录
    console.log(e)
    var that = this
    if (e.detail.value.phoneNumber == "") {
      setTimeout(function() {
        wx.showModal({
          title: '提示',
          content: '请输入你的手机号码',
          confirmText: "好的",
          confirmColor: "#F5B223",
          showCancel: false
        })
      }, 0)
    } else if (e.detail.value.phoneNumber.length != 11 || !/^1[3|4|5|6|7|8|9]\d{9}$/.test(e.detail.value.phoneNumber)) {
      wx.showModal({
        title: '提示',
        content: '请输入正确的手机号码',
        confirmText: "好的",
        confirmColor: "#F5B223",
        showCancel: false
      })
    } else if (e.detail.value.userCode == "") {
      setTimeout(function() {
        wx.showModal({
          title: '提示',
          content: '请输入你的验证码',
          confirmText: "好的",
          confirmColor: "#F5B223",
          showCancel: false
        })
      }, 0)
    } else {
      wx.cloud.callFunction({
        name: "getCode"
      }).then(res => {
        console.log("获取验证码", res)
        if (res.result.data[0].code === e.detail.value.userCode) {
          wx.cloud.callFunction({
            name: "delSms"
          }).then(res => {
            console.log("删除成功", res)
            wx.setStorage({
              key: 'phone',
              data: false
            })
            wx.setStorage({
              key: 'phoneNumber',
              data: e.detail.value.phoneNumber
            })
            wx.switchTab({
              url: '/pages/user/user'
            })
          })
        } else if (res.result.data[0].code != e.detail.value.userCode) {
          wx.showModal({
            title: '提示',
            content: '请输入正确的验证码',
            confirmText: "好的",
            confirmColor: "#F5B223",
            showCancel: false
          })
        }
      })
    }
  },
  sendCode: function(e) { //发送验证码
    wx.showLoading({
      title: "正在发送"
    })
    console.log(e)
    var that = this
    if (e.detail.value.phoneNumber == "") {
      setTimeout(function() {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '请输入你的手机号码',
          confirmText: "好的",
          confirmColor: "#F5B223",
          showCancel: false
        })
      }, 0)
    } else if (e.detail.value.phoneNumber.length != 11 || !/^1[3|4|5|6|7|8|9]\d{9}$/.test(e.detail.value.phoneNumber)) {
      wx.hideLoading()
      wx.showModal({
        title: '提示',
        content: '请输入正确的手机号码',
        confirmText: "好的",
        confirmColor: "#F5B223",
        showCancel: false
      })
    } else {
      wx.cloud.callFunction({
        name: "getPhone",
        data: {
          phoneNumber: e.detail.value.phoneNumber
        }
      }).then(res => {
        console.log("判断手机是否重号", res)
        if (res.result.data.length != 0) {
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: '该手机号已被使用，换一个手机号吧😀',
            confirmText: "好的",
            confirmColor: "#F5B223",
            showCancel: false
          })
        } else {
          wx.cloud.callFunction({
            name: 'zhenzisms',
            data: {
              $url: 'sendCode',
              apiUrl: 'https://sms.zhenzikj.com',
              message: '您的短信验证码为:{code}',
              number: e.detail.value.phoneNumber,
              messageId: '111111',
              seconds: 60,
              length: 6
            }
          }).then(res => {
            wx.hideLoading()
            that.setCode()
            console.log("获取验证码成功", res);
          }).catch(err => {
            console.log(err);
          });
        }
      })
    }
  },
  // myevent:function(e){
  //   console.log(e)
  // },
  setCode() { //验证码倒计时
    var that = this
    that.setData({
      send: true
    })
    let time = 61
    var interval = null
    interval = setInterval(function() {
      time--;
      that.setData({
        sendText: time + "秒"
      })
      if (time <= 0) {
        clearInterval(interval)
        that.setData({
          sendText: "重新发送",
          time: 61,
          send: false
        })
        wx.cloud.callFunction({
          name: "delSms"
        }).then(res => {
          console.log("删除成功", res)
        })
      }
    }, 1000)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#488fec'
    })
    wx.hideLoading()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    wx.cloud.callFunction({
      name: "delSms"
    }).then(res => {
      console.log("删除成功", res)
    })
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})